package com.atguigu.yygh.hosp.controller;


import com.atguigu.yygh.common.exception.YyghException;
import com.atguigu.yygh.common.utils.MD5;
import com.atguigu.yygh.common.utils.R;
import com.atguigu.yygh.hosp.service.HospitalSetService;
import com.atguigu.yygh.model.hosp.HospitalSet;
import com.atguigu.yygh.vo.hosp.HospitalSetQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;

/**
 * <p>
 * 医院设置表 前端控制器
 * </p>
 *
 * @author Dufeifei
 * @since 2024-06-02
 */

//@CrossOrigin//允许跨域访问
@Api(tags = "医院设置")
@RestController
@RequestMapping("/admin/hosp/hospitalSet")
public class HospitalSetController {

    @Autowired
    private HospitalSetService hospitalSetService;


    @ApiOperation("解锁与锁定")
    @PostMapping("/update/{id}/{status}")
    public R updateStatus(@ApiParam(value = "医院ID",required = true)@PathVariable Long id,
    @ApiParam(value = "状态",required = true)@PathVariable Integer status){


        //模拟异常测试全局异常处理

//
//        if(id==8){
//            int a=1/0;
//        }
//        else if(id==9)
//        {
//            String a=null;
//            a.equals("5");
//        }else if(id<0){
//            throw new YyghException(20001,"id不能为负数");
//        }


        UpdateWrapper<HospitalSet> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id",id)
                .set("status",status);

        boolean update = hospitalSetService.update(updateWrapper);

        if (update){
            return R.ok();
        }
        return R.error();
    }

    @ApiOperation("根据ID批量删除")
    @DeleteMapping("/deleteByIds")
    public R deleteByIds(@ApiParam(value = "批量删除的ID",required = true)@RequestBody List<Long> iDs ){
        boolean remove = hospitalSetService.removeByIds(iDs);
        if (remove){
            return R.ok();
        }
        return R.error();
    }


    @ApiOperation("根据ID修改")
    @PostMapping("/update")
    public R update(@ApiParam(value = "医院设置对象",required = true)@RequestBody HospitalSet hospitalSet ){
        boolean update = hospitalSetService.updateById(hospitalSet);
        if (update){
            return R.ok();
        }
        return R.error();
    }


    @ApiOperation("根据ID查询")
    @GetMapping("/{id}")
    public R getById(@ApiParam(value = "查询医院ID",required = true)@PathVariable Long id ){
        HospitalSet byId = hospitalSetService.getById(id);
        return R.ok().data("item",byId);
    }


        @ApiOperation("添加")
    @PostMapping("/add")
    public R add(@ApiParam(value = "医院设置对象")@RequestBody HospitalSet hospitalSet){

        //生成签名
        String sign=System.currentTimeMillis()+""+ new Random().nextInt(1000);
        sign = MD5.encrypt(sign);
        hospitalSet.setSignKey(sign);

        //设置状态   1  可用   0  不可用

        hospitalSet.setStatus(1);
        boolean save = hospitalSetService.save(hospitalSet);

        if (save){
            return R.ok();
        }
        return R.error();
    }
    @ApiOperation("根据条件分页查询")
    @PostMapping("/pageQuery/{page}/{size}")
    public R pageQuery(@ApiParam(value = "页码",required = true)@PathVariable Integer page,
                       @ApiParam(value = "行数",required = true)@PathVariable Integer size,
                       @ApiParam(value = "查询条件")
                           @RequestBody HospitalSetQueryVo hospitalSetQueryVo){

        //分页参数
        Page<HospitalSet> objectPage = new Page<>(page,size);

        //查询条件
        QueryWrapper<HospitalSet> hospitalSetQueryWrapper = new QueryWrapper<>();

        String hoscode = hospitalSetQueryVo.getHoscode();
        String hosname = hospitalSetQueryVo.getHosname();

        if(!StringUtils.isEmpty(hoscode)){
            hospitalSetQueryWrapper.eq("hoscode",hoscode);
        }
        if (!StringUtils.isEmpty(hosname)){
            hospitalSetQueryWrapper.like("hosname",hosname);
        }
        hospitalSetService.page(objectPage,hospitalSetQueryWrapper);
        return R.ok().data("total",objectPage.getTotal()).data("rows",objectPage.getRecords());


    }

    @ApiOperation("查询所有")
    @GetMapping("/findAll")
    public R findAll(){


        List<HospitalSet> list = hospitalSetService.list();

        return R.ok().data("list",list);
    }

    @ApiOperation("根据ID删除")
    @DeleteMapping("/{id}")
    public R  removeById (@ApiParam(value = "医院ID",required = true) @PathVariable Long id){


        boolean b = hospitalSetService.removeById(id);

        if (b){
            return R.ok();
        }
        return R.error();
    }


}

